﻿namespace EFS.RC.Wallboard.Controls.Common.Charts.Bars
{
	public partial class SingleBar : System.Windows.Controls.UserControl
	{
		#region Locals
		/// limit
		private EFS.RC.Wallboard.Controls.Common.Charts.Bars.Limit limit = null;

		/// _currentValue
		private System.Object _currentValue = 0;
		#endregion

		public SingleBar()
		{
			InitializeComponent();
		}

		#region Properties
		/// Property Title: Get, Set
		public System.String Title
		{
			get
			{
				return textBlockTitle.Text;
			}
			set
			{
				textBlockTitle.Text = value;
			}
		}

		/// Property CurrentValue: Get, Set
		public System.Object CurrentValue
		{
			get
			{
				return _currentValue;
			}
			set
			{
				this._currentValue = value;
				UpdateValue();
			}
		}

		/// Property Limit: Get, Set
		public EFS.RC.Wallboard.Controls.Common.Charts.Bars.Limit Limit
		{
			get
			{
				return limit;
			}
			set
			{
				this.limit = value;
				UpdateSizes();
			}
		}

		/// Property BarColor: Get, Set
		public System.Windows.Media.Brush BarColor
		{
			get
			{
				return borderFill.Background;
			}
			set
			{
				borderFill.Background = borderFill.BorderBrush = value;
			}
		}

		/// Property BarHeight: Get, Set
		private System.Double BarHeight
		{
			get
			{
				return uniformGridFill.Height;
			}
			set
			{
				uniformGridFill.Height = value;
			}
		}
		#endregion

		#region Utilities
		/// UpdateSizes
		private void UpdateSizes()
		{
			if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
			{
				return;
			}

			// manual measure
			this.Measure(new System.Windows.Size(System.Double.PositiveInfinity, System.Double.PositiveInfinity));
			this.Arrange(new System.Windows.Rect(new System.Windows.Point(0, 0), this.DesiredSize));

			// check limit
			EFS.Common.Verify.Reference(Limit, "Limit");

			// get current value
			System.Double currentValueTemp = 0;
			if (null != CurrentValue)
			{
				if (CurrentValue is System.TimeSpan)
				{
					currentValueTemp = ((System.TimeSpan)CurrentValue).TotalSeconds;
				}
				else
				{
					currentValueTemp = (System.Int32)CurrentValue;
				}
			}
			if (currentValueTemp < 0)
			{
				currentValueTemp = 0;
			}
			else if (currentValueTemp > Limit.Max)
			{
				currentValueTemp = Limit.Max;
			}

			// actual height
			System.Double actualHeight = this.ActualHeight - textBlockTitle.ActualHeight - textBlockValueTop.ActualHeight;
			System.Double barHeight = (actualHeight * currentValueTemp) / Limit.Max;

			// set size
			if (barHeight < borderFill.CornerRadius.TopLeft)
			{
				barHeight = borderFill.CornerRadius.TopLeft;
			}
			System.Double gridBarEmptyMarginTop = actualHeight - barHeight;
			
			// create empty margin
			System.Windows.Thickness gridBarEmptyMargin = new System.Windows.Thickness(0);
			gridBarEmptyMargin.Top = gridBarEmptyMarginTop;

			// set new margin
			gridBarEmpty.Margin = gridBarEmptyMargin;
			
			BarHeight = barHeight;

			System.Windows.Visibility textBlockValueTopVisibility = barHeight < textBlockValueBottom.ActualHeight ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
			System.Windows.Visibility textBlockValueBottomVisibility = textBlockValueTopVisibility == System.Windows.Visibility.Visible ? System.Windows.Visibility.Hidden : System.Windows.Visibility.Visible;
			if (textBlockValueTop.Visibility != textBlockValueTopVisibility)
			{
				textBlockValueTop.Visibility = textBlockValueTopVisibility;
			}	
			if (textBlockValueBottom.Visibility != textBlockValueBottomVisibility)
			{
				textBlockValueBottom.Visibility = textBlockValueBottomVisibility;
			}
		}

		/// UpdateValue
		private void UpdateValue()
		{
			if (null == CurrentValue)
			{
				return;
			}

			// switch type
			if (CurrentValue.GetType() == typeof(System.TimeSpan))
			{
				textBlockValueTop.Text = textBlockValueBottom.Text = (new System.DateTime(((System.TimeSpan)CurrentValue).Ticks)).ToString("mm:ss");
			}
			else
			{
				textBlockValueTop.Text = textBlockValueBottom.Text = CurrentValue.ToString();
			}
		}
		#endregion

		#region Events
		/// UserControl_LayoutUpdated
		private void UserControl_LayoutUpdated(object sender, System.EventArgs e)
		{
			UpdateSizes();
		}
		#endregion
	}

}